import 'package:flutter/material.dart';

typedef OnButtonResultClickListener(String text);

/// 数字安全键盘
class NumberKeyboard extends StatelessWidget {
  /// 按钮的值回调事件
  final OnButtonResultClickListener onButtonResultClickListener;
  final VoidCallback? onRightButtonClickListener;
  final VoidCallback? onLeftButtonClickListener;

  /// 按键的高度
  final double keyHeight;

  /// 文本样式
  final TextStyle textStyle;

  NumberKeyboard({
    Key? key,
    required this.onButtonResultClickListener,
    this.onRightButtonClickListener,
    this.onLeftButtonClickListener,
    this.keyHeight = 55,
    this.textStyle = const TextStyle(fontWeight: FontWeight.bold, fontSize: 16, color: Colors.black),
  }) : super(key: key);

  /// 数字按钮
  Widget _buildNumberButton(String text) {
    return GestureDetector(
      onTap: () => onButtonResultClickListener(text),
      child: Container(
        decoration: BoxDecoration(color: Colors.white, border: Border.all(color: Colors.grey[200]!, width: 0.25)),
        alignment: Alignment.center,
        child: Text(text),
      ),
    );
  }

  /// 隐藏键盘
  Widget get _buildHiddenKeyboardButton {
    return GestureDetector(
      onTap: () {
        onLeftButtonClickListener?.call();
      },
      child: Container(
        color: Color(0xFFd3d6dd),
        alignment: Alignment.center,
        child: Icon(Icons.expand_more),
      ),
    );
  }

  /// 数字按钮
  Widget get _buildClearButton {
    return GestureDetector(
      onTap: () {
        onRightButtonClickListener?.call();
      },
      child: Container(
        color: Color(0xFFd3d6dd),
        alignment: Alignment.center,
        child: Icon(Icons.backspace),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 5 * keyHeight,
      width: double.infinity,
      alignment: Alignment.bottomCenter,
      child: DefaultTextStyle(
        style: textStyle,
        child: GridView(
          physics: NeverScrollableScrollPhysics(),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            mainAxisExtent: keyHeight + 1.5,
          ),
          children: [
            _buildNumberButton("1"),
            _buildNumberButton("2"),
            _buildNumberButton("3"),
            _buildNumberButton("4"),
            _buildNumberButton("5"),
            _buildNumberButton("6"),
            _buildNumberButton("7"),
            _buildNumberButton("8"),
            _buildNumberButton("9"),
            _buildHiddenKeyboardButton,
            _buildNumberButton("0"),
            _buildClearButton,
          ],
        ),
      ),
    );
  }
}
